﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using IRUM.Core;
using IRUM.XingAPI;
using IRUM.Utils;

namespace IRUM.TR
{
    /// <summary>
    /// 야간선물 분봉
    /// </summary>
    public class t8408 : CBaseQuery
    {
        public event ChartDataHandler OnReceived;
        private List<ChartData> m_list = new List<ChartData>();

        private string focode = string.Empty;

        /// <summary>
        /// 야간선물 분봉
        /// </summary>
        /// <param name="focode">선물옵션코드, 8</param>
        public t8408(string focode)
        {
            this.focode = focode;

            SetClassData(TR_Type.t8408);
        }

        public void SetOutBlockData(int Index)
        {
            ChartData cd = new ChartData();
            cd.shcode = focode;
            cd.gubun = "M";
            cd.date = IRUM_Utils.GetLastTradingDateEvening(DateTime.Now).ToString("yyyyMMdd");    //마지막 거래일 가져와서 처리해야 함.
            cd.time = m_Query.GetFieldData(m_OutBlock1Name, "chetime", Index);
            cd.open = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "open", Index));
            cd.high = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "high", Index));
            cd.low = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "low", Index));
            cd.close = Convert.ToSingle(m_Query.GetFieldData(m_OutBlock1Name, "price", Index));
            cd.jdiff_vol = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "volume", Index)); //거래량
            cd.openyak = Convert.ToInt64(m_Query.GetFieldData(m_OutBlock1Name, "ss_mschevol", Index));

            m_list.Add(cd);
        }

        public override void HandleQuery()
        {
            int OccursCount = m_Query.GetBlockCount(m_OutBlock1Name);

            for (int OccursIndex = 0; OccursIndex < OccursCount; OccursIndex++)
            {
                try
                {
                    SetOutBlockData(OccursIndex);
                }
                catch (Exception e)
                {
                    Debug.Print(e.ToString());
                    continue;
                }
            }

            if (OnReceived != null)
            {
                OnReceived(this, new ChartDataArgs(m_list));
            }
        }

        protected override void SetRequestData()
        {
            m_Query.SetFieldData(m_InBlockName, "focode", 0, focode);  //선물옵션코드, 8
            m_Query.SetFieldData(m_InBlockName, "cgubun", 0, "B");  //일분구분, 1, T:틱, B:분
            m_Query.SetFieldData(m_InBlockName, "bgubun", 0, "001");  //분간격, 3, 0:30초, 1이상: n분
            m_Query.SetFieldData(m_InBlockName, "cnt", 0, "999");    //조회건수, 3
        }
    }
}
